////
////  CutoutCollageView.m
////  ImageStitch
////
//
//
////#import "CutoutCollageView.h"
////#import "PermissionManager.h"
////#import "RecordSaveManager.h"
////#import "ImageStitch-Swift.h"
////#import "UploadImageRequest.h"
////#import "CreateTaskRequest.h"
////#import "TaskStatusRequest.h"
////#import "VDAESUtils.h"
////#import "ImageMakeController.h"
////#import "PurchaseManager.h"
////#import "LogManager.h"
////#import "PremiumEditionController.h"
//
//
//@interface CutoutCollageView ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
//
//@property (weak, nonatomic) IBOutlet UIView *imageBgV;
//@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//@property (weak, nonatomic) IBOutlet UIButton *autoCutBtn;
//@property (weak, nonatomic) IBOutlet UIButton *lassoCutBtn;
//@property (weak, nonatomic) IBOutlet UIButton *eraseBtn;
//@property (weak, nonatomic) IBOutlet UIImageView *autoCutImgView;
//@property (weak, nonatomic) IBOutlet UIImageView *lassoCutImgView;
//@property (weak, nonatomic) IBOutlet UIImageView *eraseImgView;
//@property (weak, nonatomic) IBOutlet UILabel *autoCutLabel;
//@property (weak, nonatomic) IBOutlet UILabel *lassoCutLabel;
//@property (weak, nonatomic) IBOutlet UILabel *eraseLabel;
//
//@property (nonatomic, strong) UIBezierPath *erasePath;
//@property (nonatomic, strong) NSMutableArray<UIBezierPath *> *undoStack;
//@property (nonatomic, strong) NSMutableArray<UIBezierPath *> *restoreStack;
//@property (nonatomic, strong) UIImage *originalMask;
//@property (nonatomic, strong) UIImage *sourceImage;
//@property (nonatomic, assign) CGPoint lastTouchPoint;
//@property (nonatomic, strong) UIImageView *eraserIconView;
//@property (nonatomic, strong) UIImage *cutImage;
//
//@property (nonatomic, assign) BOOL isRestoring;
//@property (nonatomic, assign) BOOL isEdit;
//@property (nonatomic, assign) int taskCount;
//
//@property (nonatomic, assign) BOOL isPinching;
//
//@end
//
//@implementation CutoutCollageView
//
//- (void)awakeFromNib {
//    [super awakeFromNib];
//    
//    // 开启用户交互
//    self.imageView.userInteractionEnabled = YES;
//
//    // 添加捏合手势
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
//    [self.imageView addGestureRecognizer:pinch];
//
//    // 添加拖拽手势（仅支持双指平移）
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
//    pan.minimumNumberOfTouches = 2;
//    pan.maximumNumberOfTouches = 2;
//    [self.imageView addGestureRecognizer:pan];
//}
//
//- (void)setImage:(UIImage *)image {
//    _image = image;
//    
//    self.sourceImage = image;
//    self.imageView.image = image;
//    
//    [self autocutBtnClick:self.autoCutBtn];
//}
//
//#pragma mark - Button Click
//- (IBAction)autocutBtnClick:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    
//    _lassoCutBtn.selected = NO;
//    _eraseBtn.selected = NO;
//    _isEdit = NO;
//    if (self.eraserIconView) {
//        self.eraserIconView.hidden = YES;
//    }
//
//    _autoCutImgView.image = [UIImage imageNamed:sender.selected?@"make_autocut_sel":@"make_autocut_nor"];
//    _lassoCutImgView.image = [UIImage imageNamed:@"make_lassocut_nor"];
//    _eraseImgView.image = [UIImage imageNamed:@"make_erase_nor"];
//
//    _autoCutLabel.textColor = [UIColor colorWithHexString:sender.selected?@"#9684FF":@"#141415"];
//    _lassoCutLabel.textColor = [UIColor colorWithHexString:@"#141415"];
//    _eraseLabel.textColor = [UIColor colorWithHexString:@"#141415"];
//
//    if (sender.selected) {
////        [self segmentPersonUsingBackgroundRemoval:self.sourceImage completion:^(UIImage *outputImage) {
////            dispatch_async(dispatch_get_main_queue(), ^{
////                self.imageView.image = outputImage;
////            });
////        }];
//        if (self.cutImage) {
//            self.imageView.image = self.cutImage;
//            self.originalMask = self.cutImage;
//            
//            [self resetTouchData];
//        } else {
//            [self loadResultData:self.sourceImage];
//        }
//        
//    } else {
//        self.imageView.image = self.sourceImage;
//        self.originalMask = self.sourceImage;
//        
//        [self resetTouchData];
//    }
//}
//
//- (IBAction)lassoCutBtnClick:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    _eraseBtn.selected = NO;
//    _isEdit = sender.selected;
//    self.eraserIconView.hidden = !sender.selected;
//
//    _lassoCutImgView.image = [UIImage imageNamed:sender.selected?@"make_lassocut_sel":@"make_lassocut_nor"];
//    _eraseImgView.image = [UIImage imageNamed:@"make_erase_nor"];
//    _lassoCutLabel.textColor = [UIColor colorWithHexString:sender.selected?@"#9684FF":@"#141415"];
//    _eraseLabel.textColor = [UIColor colorWithHexString:@"#141415"];
//
//    if (!sender.selected) return;
//
//    self.isRestoring = YES;
//    self.originalMask = self.imageView.image;
//    
//    
//    
////    // 显示原图，并对未抠区域叠加半透明黑色蒙版
////    UIGraphicsBeginImageContextWithOptions(self.sourceImage.size, NO, 0);
////    [self.sourceImage drawAtPoint:CGPointZero];
////
////    CGContextRef ctx = UIGraphicsGetCurrentContext();
////    CGContextSaveGState(ctx);
////
////    // 使用抠图蒙版的 alpha 通道作为剪切区域
////    CGImageRef maskImageRef = self.originalMask.CGImage;
////    CGRect imageRect = CGRectMake(0, 0, self.sourceImage.size.width, self.sourceImage.size.height);
////    CGContextClipToMask(ctx, imageRect, maskImageRef);
////
////    // 先剪掉已抠区域，绘制高亮覆盖未抠区域
////    CGContextAddRect(ctx, imageRect);
////    CGContextClip(ctx);
////
////    [[UIColor colorWithHexString:@"#0095FF" alpha:0.6] setFill];
////    UIRectFillUsingBlendMode(imageRect, kCGBlendModeNormal);
////
////    CGContextRestoreGState(ctx);
////
////    UIImage *highlighted = UIGraphicsGetImageFromCurrentImageContext();
////    UIGraphicsEndImageContext();
////
////    self.imageView.image = highlighted;
//
//    
//    
//    
////    // 先显示原图
////    self.originalMask = self.sourceImage;
////    // 在原图上叠加当前抠图结果作为半透明高亮
////    UIGraphicsBeginImageContextWithOptions(self.cutImage.size, NO, 0);
////    [self.cutImage drawAtPoint:CGPointZero];
////    [[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.5] setFill];
////    [self.sourceImage drawAtPoint:CGPointZero blendMode:kCGBlendModeSourceIn alpha:1.0];
////    UIImage *highlighted = UIGraphicsGetImageFromCurrentImageContext();
////    UIGraphicsEndImageContext();
////    self.imageView.image = highlighted;
//
//    
//    
//
//    if (!self.eraserIconView) {
////        self.eraserIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"make_cut_line"]];
////        self.eraserIconView.bounds = CGRectMake(0, 0, 32, 32);
//        self.eraserIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
//        self.eraserIconView.image = [UIImage imageNamed:@"make_cut_line"];
//        self.eraserIconView.center = self.imageView.center;
//        [self.imageView addSubview:self.eraserIconView];
//    }
//}
//
//- (IBAction)eraseBtnClick:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    _lassoCutBtn.selected = NO;
//    _isEdit = sender.selected;
//    self.eraserIconView.hidden = !sender.selected;
//
//    _lassoCutImgView.image = [UIImage imageNamed:@"make_lassocut_nor"];
//    _eraseImgView.image = [UIImage imageNamed:sender.selected?@"make_erase_sel":@"make_erase_nor"];
//    _lassoCutLabel.textColor = [UIColor colorWithHexString:@"#141415"];
//    _eraseLabel.textColor = [UIColor colorWithHexString:sender.selected?@"#9684FF":@"#141415"];
//
//    if (!sender.selected) return;
//
//    self.isRestoring = NO;
//    self.originalMask = self.imageView.image;
//
//    if (!self.eraserIconView) {
////        self.eraserIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"make_cut_line"]];
////        self.eraserIconView.bounds = CGRectMake(0, 0, 20, 20);
//        self.eraserIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
//        self.eraserIconView.image = [UIImage imageNamed:@"make_cut_line"];
//        self.eraserIconView.center = self.imageView.center;
//        [self.imageView addSubview:self.eraserIconView];
//    }
//}
//
//#pragma mark - btnClick
//- (IBAction)backBtnClick:(UIButton *)sender {
//    [self removeFromSuperview];
//    
//    if (self.backBtnClick) {
//        self.backBtnClick();
//    }
//}
//
//- (IBAction)saveBtnClick:(UIButton *)sender {
//    if (![[PurchaseManager shareInstance] isMember] &&
//        [CommonClass showCutout]) {
//        PremiumEditionController *vc = [[PremiumEditionController alloc] init];
////        [self.viewController.navigationController pushViewController:vc animated:YES];
//        UIViewController *currentVC = [CommonClass currentViewController];
//        [currentVC.navigationController pushViewController:vc animated:YES];
//
//    } else {
//        if (self.imageView.image) {
//            [CommonClass updateCutout:YES];
//        }
//        
//        if (self.saveBtnClick) {
//            self.saveBtnClick(self.imageView.image);
//        }
//    }
//}
//
//#pragma mark - Touch 轨迹处理
//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    if (!_isEdit || self.isPinching) return;
//
//    UITouch *touch = [touches anyObject];
//    CGPoint touchPointInView = [touch locationInView:self.imageView];
//
//    UIImage *img = self.sourceImage;
//    if (!img) return;
//
//    CGSize imageSize = img.size;
//    CGSize imageViewSize = self.imageView.bounds.size;
//    CGFloat scale = MIN(imageViewSize.width / imageSize.width, imageViewSize.height / imageSize.height);
//    CGSize fittedImageSize = CGSizeMake(imageSize.width * scale, imageSize.height * scale);
//    CGFloat x = (imageViewSize.width - fittedImageSize.width) / 2.0;
//    CGFloat y = (imageViewSize.height - fittedImageSize.height) / 2.0;
//
//    CGPoint currentPoint = CGPointMake(
//        (touchPointInView.x - x) * (imageSize.width / fittedImageSize.width),
//        (touchPointInView.y - y) * (imageSize.height / fittedImageSize.height)
//    );
//
//    if (self.eraserIconView) {
//        self.eraserIconView.center = touchPointInView;
//    }
//
//    if (!self.originalMask) return;
//
//    if (!self.erasePath) {
//        self.erasePath = [UIBezierPath bezierPath];
//        self.erasePath.lineWidth = 5.0;
//        self.erasePath.lineCapStyle = kCGLineCapRound;
//    }
//
//    if (CGPointEqualToPoint(self.lastTouchPoint, CGPointZero)) {
//        [self.erasePath moveToPoint:currentPoint];
//    } else {
//        CGPoint midPoint = CGPointMake(
//            (self.lastTouchPoint.x + currentPoint.x) / 2.0,
//            (self.lastTouchPoint.y + currentPoint.y) / 2.0
//        );
//        [self.erasePath addQuadCurveToPoint:midPoint controlPoint:self.lastTouchPoint];
//    }
//
//    self.lastTouchPoint = currentPoint;
//
//    [self renderMask];
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    if (!_isEdit || self.isPinching) return;
//
//    if (self.erasePath && !self.erasePath.empty) {
//        if (self.isRestoring) {
//            if (!self.restoreStack) {
//                self.restoreStack = [NSMutableArray array];
//            }
//            [self.restoreStack addObject:[self.erasePath copy]];
//            // 恢复操作，清除擦除轨迹
//            [self.undoStack removeAllObjects];
//        } else {
//            if (!self.undoStack) {
//                self.undoStack = [NSMutableArray array];
//            }
//            [self.undoStack addObject:[self.erasePath copy]];
//            // 擦除操作，清除恢复轨迹
//            [self.restoreStack removeAllObjects];
//        }
//    }
//    self.erasePath = nil;
//    self.lastTouchPoint = CGPointZero;
//}
//
//// 根据当前 originalMask 叠加擦除/恢复操作，仅沿轨迹绘制线条
//- (void)renderMask {
//    UIGraphicsBeginImageContextWithOptions(self.sourceImage.size, NO, 0);
//    [self.originalMask drawAtPoint:CGPointZero];
//
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    CGContextSaveGState(ctx);
//
//    CGContextSetLineCap(ctx, kCGLineCapRound);
//    CGContextSetLineWidth(ctx, 20.0);
//
//    if (self.isRestoring) {
//        for (UIBezierPath *p in self.restoreStack) {
//            CGContextSaveGState(ctx);
//            CGContextAddPath(ctx, p.CGPath);
//            CGContextReplacePathWithStrokedPath(ctx);
//            CGContextClip(ctx);
//            [self.sourceImage drawAtPoint:CGPointZero];
//            CGContextRestoreGState(ctx);
//        }
//        if (self.erasePath) {
//            CGContextSaveGState(ctx);
//            CGContextAddPath(ctx, self.erasePath.CGPath);
//            CGContextReplacePathWithStrokedPath(ctx);
//            CGContextClip(ctx);
//            [self.sourceImage drawAtPoint:CGPointZero];
//            CGContextRestoreGState(ctx);
//        }
//    } else {
//        CGContextSetBlendMode(ctx, kCGBlendModeClear);
//        CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
//        NSArray<UIBezierPath *> *paths = self.undoStack;
//        for (UIBezierPath *p in paths) {
//            CGContextAddPath(ctx, p.CGPath);
//            CGContextStrokePath(ctx);
//        }
//        if (self.erasePath) {
//            CGContextAddPath(ctx, self.erasePath.CGPath);
//            CGContextStrokePath(ctx);
//        }
//    }
//
//    CGContextRestoreGState(ctx);
//
//    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//
//    self.imageView.image = result;
//    self.originalMask = result;
//}
//
//- (void)resetTouchData {
//    [self.undoStack removeAllObjects];
//    [self.restoreStack removeAllObjects];
//    self.erasePath = nil;
//    self.lastTouchPoint = CGPointZero;
//}
//
//#pragma mark - Album
//- (void)albumClickEvent {
//    [PermissionManager checkPhotoLibraryPermission];
//    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
//    imagePickerController.delegate = self;
//    imagePickerController.allowsEditing = NO;
//    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//        [self.viewController presentViewController:imagePickerController animated:YES completion:nil];
//    }
//}
//
//#pragma mark - UIImagePickerController
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    
//    [self.viewController.navigationController popViewControllerAnimated:YES];
//}
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    if (image) {
//        self.sourceImage = image;
//        self.imageView.image = image;
//        [self autocutBtnClick:self.autoCutBtn];
//    }
//}
//
//- (void)loadResultData:(UIImage *)image {
//    if (!image) {
//        [PSProgressHUD showToast:@"The image cannot be empty" view:self];
//        return;
//    }
//    
//    [PSProgressHUD showInView:self];
//    
//    UploadImageRequest *requset = [[UploadImageRequest alloc] init];
//    requset.image = image;
//    
//    @weakify(self)
//    [requset ps_startWithCompletionBlockWithSuccess:^(__kindof PSRequest *request) {
//        @strongify(self)
//        NSDictionary *responseDict = request.responseObject;
//        if ([[responseDict objectForKey:@"code"] isEqualToNumber:@0]) {
//            NSString *filePath = [[responseDict objectForKey:@"data"] objectForKey:@"filePath"];
//            [self createTask:filePath];
//        } else {
//            [PSProgressHUD hideInView:self];
//            [PSProgressHUD showToast:@"Please try again later" view:self];
//        }
//        
//        DLog(@"loadResultData--successsuccess--:%@", responseDict);
//        
//    } failure:^(__kindof PSRequest *request, NSString *message) {
//        @strongify(self)
//        [PSProgressHUD hideInView:self];
//        [PSProgressHUD showToast:@"Please try again later" view:self];
//        DLog(@"loadResultData--errorerror--:%@", message);
//    }];
//}
//
//- (void)createTask:(NSString *)filePath {
//    CreateTaskRequest *requset = [[CreateTaskRequest alloc] init];
//    requset.imageUrl = filePath;
//    requset.taskType = @"PORTRAIT_MATTING";
//    
//    @weakify(self)
//    [requset ps_startWithCompletionBlockWithSuccess:^(__kindof PSRequest *request) {
//        @strongify(self)
//        NSData *responseData = request.responseObject;
//        NSString *responseStr = [VDAESUtils decryptECBString:[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ithKey:@"32NnHA0t2d3SKX25lMOzLQ=="];
//        NSDictionary *responseDict = [responseStr mj_JSONObject];
//        if ([[responseDict objectForKey:@"code"] isEqualToNumber:@0]) {
//            NSString *taskId = [[responseDict objectForKey:@"data"] objectForKey:@"taskId"];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self taskStatus:taskId];
//            });
//        } else {
//            [PSProgressHUD hideInView:self];
//            [PSProgressHUD showToast:@"Please try again later" view:self];
//        }
//        
//        DLog(@"createTask--successsuccess--:%@", responseDict);
//        
//    } failure:^(__kindof PSRequest *request, NSString *message) {
//        @strongify(self)
//        [PSProgressHUD hideInView:self];
//        [PSProgressHUD showToast:@"Please try again later" view:self];
//        DLog(@"createTask--errorerror--:%@", message);
//    }];
//}
//
//- (void)taskStatus:(NSString *)taskId {
//    _taskCount++;
//    if (_taskCount>180) {
//        [PSProgressHUD hideInView:self];
//        _taskCount = 0;
//        return;
//    }
//    
//    TaskStatusRequest *requset = [[TaskStatusRequest alloc] init];
//    requset.taskId = taskId;
//    
//    @weakify(self)
//    [requset ps_startWithCompletionBlockWithSuccess:^(__kindof PSRequest *request) {
//        @strongify(self)
//        NSData *responseData = request.responseObject;
//        NSString *responseStr = [VDAESUtils decryptECBString:[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ithKey:@"32NnHA0t2d3SKX25lMOzLQ=="];
//        NSDictionary *responseDict = [responseStr mj_JSONObject];
//        DLog(@"taskStatus--successsuccess--:%@", responseDict);
//        if ([[responseDict objectForKey:@"code"] isEqualToNumber:@0]) {
//            // WAITING，PROCESSING，SUCCESS，FAIL，EXPIRED，
//            NSString *status = [[responseDict objectForKey:@"data"] objectForKey:@"status"];
//            if ([status isEqualToString:@"WAITING"] ||
//                [status isEqualToString:@"PROCESSING"]) {
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [self taskStatus:taskId];
//                });
//            } else if ([status isEqualToString:@"SUCCESS"]) {
//                DLog(@"taskStatus--SUCCESS--");
//                NSArray *resultArray = [[responseDict objectForKey:@"data"] objectForKey:@"resUrls"];
//                dispatch_async(dispatch_get_main_queue(), ^{
////                    [self.imageView sd_setImageWithURL:[NSURL URLWithString:resultArray.lastObject] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
////                        [PSProgressHUD hideInView:self];
////                        if (image) {
////                            self.cutImage = image;
////                        }
////                    }];
//                    [self.imageView sd_setImageWithURL:[NSURL URLWithString:resultArray.lastObject] placeholderImage:nil options:SDWebImageProgressiveLoad completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//                        [PSProgressHUD hideInView:self];
//                        if (image) {
//                            self.cutImage = image;
//                        }
//                    }];
//                });
//            } else {
//                [PSProgressHUD hideInView:self];
//                [PSProgressHUD showToast:@"Please try again later" view:self];
//            }
//        } else {
//            [PSProgressHUD hideInView:self];
//            [PSProgressHUD showToast:@"Please try again later" view:self];
//        }
//    } failure:^(__kindof PSRequest *request, NSString *message) {
//        @strongify(self)
//        [PSProgressHUD hideInView:self];
//        [PSProgressHUD showToast:@"Please try again later" view:self];
//        DLog(@"taskStatus--errorerror--:%@", message);
//    }];
//}
//
//#pragma mark - Gesture
//- (void)handlePinch:(UIPinchGestureRecognizer *)pinch {
//    if (pinch.state == UIGestureRecognizerStateBegan ||
//        pinch.state == UIGestureRecognizerStateChanged) {
//        self.isPinching = YES;
//    } else if (pinch.state == UIGestureRecognizerStateEnded ||
//               pinch.state == UIGestureRecognizerStateCancelled) {
//        self.isPinching = NO;
//    }
//    // 在当前 transform 上叠加缩放
//    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//    pinch.scale = 1.0; // 重置 scale，避免叠加过度
//    
//    [self resetTouchData];
//}
//
//- (void)handlePan:(UIPanGestureRecognizer *)pan {
//    UIView *piece = pan.view;
//    CGPoint translation = [pan translationInView:piece.superview];
//    if (pan.state == UIGestureRecognizerStateBegan ||
//        pan.state == UIGestureRecognizerStateChanged) {
//        piece.center = CGPointMake(piece.center.x + translation.x, piece.center.y + translation.y);
//        [pan setTranslation:CGPointZero inView:piece.superview];
//        self.isPinching = YES; // 复用 isPinching 标志禁止绘制
//    } else if (pan.state == UIGestureRecognizerStateEnded ||
//               pan.state == UIGestureRecognizerStateCancelled) {
//        self.isPinching = NO;
//    }
//    [self resetTouchData];
//}
//
//#pragma mark - Swift Wrapper 调用 【原位对齐版】
////- (void)segmentPersonUsingBackgroundRemoval:(UIImage *)inputImage completion:(void (^)(UIImage *outputImage))completion {
////    if (!inputImage) return;
////
////    CGSize modelInputSize = CGSizeMake(512, 512);
////
////    CGFloat scale = MIN(modelInputSize.width / inputImage.size.width,
////                        modelInputSize.height / inputImage.size.height);
////    CGSize scaledSize = CGSizeMake(inputImage.size.width * scale, inputImage.size.height * scale);
////    CGFloat offsetX = (modelInputSize.width - scaledSize.width) / 2.0;
////    CGFloat offsetY = (modelInputSize.height - scaledSize.height) / 2.0;
////
////    UIGraphicsBeginImageContextWithOptions(modelInputSize, NO, 1.0);
////    [[UIColor blackColor] setFill];
////    UIRectFill(CGRectMake(0, 0, modelInputSize.width, modelInputSize.height));
////    CGRect drawRect = CGRectMake(offsetX, offsetY, scaledSize.width, scaledSize.height);
////    [inputImage drawInRect:drawRect];
////    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
////    UIGraphicsEndImageContext();
////
////    BackgroundRemovalWrapper *wrapper = [[BackgroundRemovalWrapper alloc] init];
////    UIImage *rawOutput = [wrapper removeBackgroundFrom:scaledImage];
////    if (!rawOutput) return;
////
////    CGRect cropRect = CGRectMake(offsetX * rawOutput.scale, offsetY * rawOutput.scale, scaledSize.width * rawOutput.scale, scaledSize.height * rawOutput.scale);
////    CGImageRef cropped = CGImageCreateWithImageInRect(rawOutput.CGImage, cropRect);
////    UIImage *croppedMask = [UIImage imageWithCGImage:cropped scale:rawOutput.scale orientation:rawOutput.imageOrientation];
////    CGImageRelease(cropped);
////
////    UIGraphicsBeginImageContextWithOptions(inputImage.size, NO, inputImage.scale);
////    [croppedMask drawInRect:CGRectMake(0, 0, inputImage.size.width, inputImage.size.height)];
////    UIImage *finalMask = UIGraphicsGetImageFromCurrentImageContext();
////    UIGraphicsEndImageContext();
////
////    self.originalMask = finalMask;
////    self.sourceImage = inputImage;
////    self.erasePath = [UIBezierPath bezierPath];
////    self.undoStack = [NSMutableArray array];
////    self.restoreStack = [NSMutableArray array];
////
////    if (finalMask && completion) {
////        completion(finalMask);
////    }
////}
//
//@end
